package org.linlinjava.litemall.wx.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LemonlaUserPoint;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.UserVo;
import org.linlinjava.litemall.db.service.LemonlaUserPointService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.HomeCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.linlinjava.litemall.wx.util.WxResponseCode.ALREADY_CHECKED;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/wx/user")
@Validated
public class WxUserController {
    private final Log logger = LogFactory.getLog(WxUserController.class);

    @Autowired
    private LitemallOrderService orderService;
    @Autowired
    private LemonlaUserPointService userPointService;

    @Autowired
    private LitemallUserService userService;
    /**
     * 用户个人页面数据
     * <p>
     * 目前是用户订单统计信息
     *
     * @param userId 用户ID
     * @return 用户个人页面数据
     */
    @GetMapping("index")
    public Object list(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("order", orderService.orderInfo(userId));
        return ResponseUtil.ok(data);
    }

    @PostMapping("update")
    public Object update(@LoginUser Integer userId,@RequestBody String body) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        LitemallUser user = userService.findById(userId);
        String nickname = JacksonUtil.parseString(body, "nickname");
        String  avatar = JacksonUtil.parseString(body, "avatar");
        String  mobile = JacksonUtil.parseString(body, "mobile");
        if (StringUtils.isNotEmpty(nickname)) {
            user.setNickname(nickname);
        }
        if (StringUtils.isNotEmpty(avatar)) {
            user.setAvatar(avatar);
        }
        if (StringUtils.isNotEmpty(mobile)) {
            user.setMobile(mobile);
        }
        userService.updateById(user);
        UserVo uservo = userService.findUserVoById(userId);
        return ResponseUtil.ok(uservo);
    }

    @GetMapping("get")
    public Object get(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        UserVo uservo = userService.findUserVoById(userId);
        return ResponseUtil.ok(uservo);
    }





    @GetMapping("userpoints")
    public Object userpoints(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        LemonlaUserPoint userpoint = userPointService.findByUserId(userId);
        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("points", userpoint.getPoint());
        return ResponseUtil.ok(data);
    }

    @PostMapping("dailycheck")
    public Object dailycheck(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Date date = new Date();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateformate = sdf.format(date);
        boolean success = HomeCacheManager.checkDaily(userId, dateformate);
        if (success) {
            LemonlaUserPoint userpoint = userPointService.findByUserId(userId);
            userpoint.setPoint(userpoint.getPoint() + 200);
            userPointService.updateById(userpoint);
        } else {
            return ResponseUtil.fail(ALREADY_CHECKED,"今天已经领取过了");
        }

        return ResponseUtil.ok();
    }

    @PostMapping("adcheck")
    public Object adcheck(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Date date = new Date();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateformate = sdf.format(date);
//        boolean success = HomeCacheManager.checkAd(userId, dateformate);
            LemonlaUserPoint userpoint = userPointService.findByUserId(userId);
            userpoint.setPoint(userpoint.getPoint() + 1000);
            userPointService.updateById(userpoint);


        return ResponseUtil.ok();
    }


    @PostMapping("sharecheck")
    public Object sharecheck(@LoginUser Integer userId) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Date date = new Date();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateformate = sdf.format(date);
        boolean success = HomeCacheManager.checkshare(userId, dateformate);
        if (success) {
            LemonlaUserPoint userpoint = userPointService.findByUserId(userId);
            userpoint.setPoint(userpoint.getPoint() + 500);
            userPointService.updateById(userpoint);
        } else {
            return ResponseUtil.fail(ALREADY_CHECKED,"今天已经领取过了");
        }

        return ResponseUtil.ok();
    }

}